package dm.ast;


import test.envset.GlobalState;
import test.utility.Utility;

/**
 * 常量表达式
 * 各种类型的常量表达式，例如null ,数值型，字符型，boolean类型等等
 * 处于AST中的叶子节点
 * 不包含运算符
 */
public class DmConstant extends DmExpression{
    public DmConstant() {
        isCompleted = true;  //AST中叶子节点，可以直接转化为sql语句的一部分
        isLeaf = true;
        setComplexity();
        setTargetWidt(1);
        setTargetDept(1);
    }

    @Override
    public void setWidt(){
        wid_t = 1;
    }

    @Override
    public void setDept(){
        dep_t = 1;
    }

    public static class NullDmConst extends DmConstant{
        @Override
        public DmDataType getExpressionType(){
            return null;
        }

        @Override
        public String toSql(){
            return "NULL";
        }
    }

    public static class BooleanDmConst extends DmConstant{
        private final boolean value;

        public BooleanDmConst(boolean value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.BOOLEAN;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return value ? "1" : "0";
        }
    }

    public static class StringDmConst extends DmConstant{
        private final String value;

        public StringDmConst(String value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.CHAR;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.format("'%s'" , value.replace("'" , "''"));
        }
    }

    public static class IntDmConst extends DmConstant{
        private final long value;

        public IntDmConst(long value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.INT;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.valueOf(value);
        }
    }

    public static class BinaryDmConst extends DmConstant{
        private final String value;

        public BinaryDmConst(String value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.BINARY;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.format("'%s" , Utility.stringToHexString(value));
        }
    }

    public static class TimeDmConst extends DmConstant{
        private final long value;  //表示时间戳

        public TimeDmConst(long value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.TIME;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.format("'%s" , Utility.longToTime(value));
        }
    }

    public static class IntervalDmConst extends DmConstant{
        private final int value;

        public IntervalDmConst(int value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.INTERVAL;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.format(" interval '%d' year" , value);
        }
    }

    public static class ClobDmConst extends DmConstant{
        private final String value;

        public ClobDmConst(String value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.CLOB;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.format("'%s" , value.replace("'" , "''"));
        }
    }

    public static class BlobDmConst extends DmConstant{
        private final String value;

        public BlobDmConst(String value){
            this.value = value;
        }

        @Override
        public DmDataType getExpressionType(){
            return DmDataType.BLOB;
        }

        @Override
        public String toSql(){
            //Todo Auto-generatd method stub
            return String.format("'%s" , Utility.stringToHexString(value));
        }
    }
    //转化为字符串形式
    @Override
    public String toSql(){
        return " " + this.getClass().getSimpleName();
    }

    @Override
    public void fill(GlobalState globalState){
        //NOTHING
    }
}
